﻿using Hr.IDAL;
using Hr.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Hr.DAL
{
    public class NoticeDAL : BaseDAL<Notice_List>, INoticeDAL
    {
        private DbContext db = DbContextFactory.Create();
        /// <summary>
        /// 获取通知管理信息,模糊查询
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object> GetNoticeList(string Notices)
        {
            if (Notices == null)
            {
                Notices = "";
            }
            return (
                from Notice in db.Set<Notice_List>()
                join User in db.Set<User_List>() on Notice.fk_User equals User.pk_User
                join Department in db.Set<User_Department>() on Notice.fk_Department equals Department.pk_Department
                orderby Notice.CreateDate descending
                where Notice.Title.Contains(Notices)
                select new {
                    StatusName_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Notice_list") & n.ID == Notice.StatusID).FirstOrDefault().StatusName_CHS,
                    TypeName_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Notice_list") & n.ID == Notice.TypeID).FirstOrDefault().TypeName_CHS,
                    Visual=db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Notice_list") & n.ID == Notice.TypeID).FirstOrDefault().Visual,
                    Notice.pk_Notice,
                    Notice.Title,
                    Department.DepartmentName,
                    Notice.CreateDate,
                    User.FullName,
                    Notice.IsTop,
                    Notice.FilePath

                }
                ) ;
        }

        /// <summary>
        /// 获取公告管理类型
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetMyModels<T>(Expression<Func<T,bool>> whereLabmal) where T : class
        {
            return db.Set<T>().Where(whereLabmal);
        }

        /// <summary>
        /// 获取通知列表信息，模糊查询
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object> GetViewLIist(string Title)
        {
            if (Title == null)
            {
                Title = "";
            }
            return (
               from Notice in db.Set<Notice_List>()
               join User in db.Set<User_List>() on Notice.fk_User equals User.pk_User
               join Department in db.Set<User_Department>() on Notice.fk_Department equals Department.pk_Department
               orderby Notice.CreateDate descending
               where Notice.Title.Contains(Title)
               select new
               {
                   Notice.pk_Notice,                
                   Notice.Title,
                   Department.DepartmentName,
                   Notice.CreateDate,
                   User.FullName,
                   Notice.FilePath,
                   StatusName_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Notice_List") & n.ID == Notice.StatusID).FirstOrDefault().StatusName_CHS,
                   TypeName_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Notice_List") & n.ID == Notice.TypeID).FirstOrDefault().TypeName_CHS,
                   Visual = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Notice_list") & n.ID == Notice.TypeID).FirstOrDefault().Visual,

               }
               );
        }

        /// <summary>
        /// 根据Id获取通知管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Notice_List GetNoticeInfo(int id)
        {
            return db.Set<Notice_List>().SingleOrDefault(n => n.pk_Notice == id);
        }

        /// <summary>
        /// 查看公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetNoticDesc(int id)
        {
            return (from NL in db.Set<Notice_List>()
                    join UD in db.Set<User_Department>()
                    on NL.fk_Department equals UD.pk_Department
                    where NL.pk_Notice==id
                    select new
                    {
                        NL.pk_Notice,
                        NL.Title,
                        NL.Description,
                        UD.DepartmentName,
                        NL.CreateDate
                    }).FirstOrDefault();
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public List<Notice_List> GetNoticeAll()
        {
            return db.Set<Notice_List>().ToList();
        }

        /// <summary>
        /// 添加，修改公告管理
        /// </summary>
        /// <param name="n"></param>
        /// <param name="Type"></param>
        public void UpdateNotice(Notice_List nl,string Type,int pk_User)
        {         
            if (Type.Equals("Add"))
            {
                nl.CreateDate = Convert.ToDateTime(DateTime.Now.ToString());
                nl.fk_User = pk_User;
                nl.fk_Department =db.Set<User_List>().Where(n=>n.pk_User==pk_User).SingleOrDefault().fk_Department;
                nl.CreateUser = pk_User;
                db.Set<Notice_List>().Add(nl);
            }
            else if (Type.Equals("Edit"))           
            {
                nl.ModifyDate = DateTime.Now;
                nl.ModifyUser = pk_User;
                db.Set<Notice_List>().AddOrUpdate(nl);
            }
        }
      
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}
